﻿Partial Class ViewProfile
    Inherits System.Web.UI.Page
    Public dbmanager = New DBManager

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        'Load
        Dim display_name = Request.QueryString("id")
        Try
            Dim userProfile As userAcc = dbmanager.retrieveUser(display_name)
            lblDisplayEmail.Text = userProfile.email
            tbxDisplayName.Text = userProfile.display_name
            lblDisplayGender.Text = userProfile.gender
            Try
                tbxFirstName.Text = userProfile.first_name
            Catch ex As Exception
                tbxFirstName.Text = ""
            End Try
            Try
                tbxLastName.Text = userProfile.last_name
            Catch ex As Exception
                tbxLastName.Text = ""
            End Try
            If userProfile.date_of_birth.ToString = "1/1/0001 AM 12:00:00" Then
                tbxDOB.Text = ""
            Else
                tbxDOB.Text = userProfile.date_of_birth.ToShortDateString
            End If
            Try
                ddlCountry.Text = userProfile.country
            Catch ex As Exception
                ddlCountry.Text = "Unselected"
            End Try
        Catch ex As Exception
            MsgBox("Please login first", MsgBoxStyle.OkOnly, "Please login")
            Response.Redirect("home.aspx")
        End Try
        btnConfirm.Visible = False

    End Sub

    Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDelete.Click
        Dim display_name = tbxDisplayName.Text
        If MsgBox("Are you sure you want to delete your account?", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then
            dbmanager.deleteUser(display_name)
            MsgBox("Account Deleted")
            Response.Redirect("home.aspx")
        End If
    End Sub

    Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpdate.Click
        tbxDisplayName.Enabled = True
        tbxPassword.Enabled = True
        tbxRetypePassword.Enabled = True
        tbxFirstName.Enabled = True
        tbxLastName.Enabled = True
        tbxDOB.Enabled = True
        ddlCountry.Enabled = True
        btnConfirm.Visible = True
        btnUpdate.Visible = False
        'tbxPassword.TextMode = TextBoxMode.Password
        'tbxRetypePassword.TextMode = TextBoxMode.Password
    End Sub

    Protected Sub btnConfirm_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnConfirm.Click
        'Dim temp_date As DateFormat

        Try
            dbmanager.updateUser(tbxDisplayName.Text, tbxFirstName.Text, tbxLastName.Text, tbxDOB.Text, ddlCountry.Text)
            MsgBox("You have updated your profile!")
        Catch ex As Exception
            'Try
            '    dbmanager.updateUser(tbxDisplayName.Text, tbxFirstName.Text, tbxLastName.Text, temp_date, ddlCountry.Text)
            '    MsgBox("You have updated your profile!")
            'Catch ex2 As Exception
            MsgBox("You have not updated your profile successfully")
            'End Try
        End Try

        tbxFirstName.Enabled = False
        tbxDisplayName.Enabled = False
        tbxPassword.Enabled = False
        tbxRetypePassword.Enabled = False
        tbxFirstName.Enabled = False
        tbxLastName.Enabled = False
        tbxDOB.Enabled = False
        ddlCountry.Enabled = False
        btnConfirm.Visible = False
        btnUpdate.Visible = True
    End Sub

    Protected Sub btnChange_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDelete.Click
        dbmanager.change(tbxPassword.Text)
        MsgBox("Your new password is changed")
    End Sub
End Class